package C705;

import java.util.HashMap;
import java.util.Map;

/**
 * @program: javase
 * @description: 使用map实现单例
 * @author: luolidong
 * @create: 2021-07-5 17:38
 * @version: 1.1
 */
public class SingletonMap {
    //创建map
    private static Map<String, SingletonMap> map = new HashMap<>();

    //构造器私有
    private SingletonMap() {
    }

    /**
     * @Description: 获取单例对象
     * @Param: [clazz] 类名
     * @return: SingletonMap 实例对象
     */
    public static SingletonMap getInstance(String clazz) {
        if (!map.containsKey(clazz)) {
            try {
                Class aClass = Class.forName(clazz);
                SingletonMap o = (SingletonMap) aClass.newInstance();
                map.put(clazz, o);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map.get(clazz);
    }
}
